<?php
/* MyMeeting is an online meeting management software.
 * Copyright (C) 2009 Government Of Malaysia
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * @author: Abdullah Zainul Abidin, Nuhaa All Bakry
 *          Eavay Javay Barnad, Sarogini Muniyandi, Teow Jit Huan
 *
 */

class AjaxesController extends AppController {
    /**
     * Define $uses
     *
     */
    var $uses=array('Item','User','Group','Committee','UsersGroup');
    /**
     * Define $helpers
     *
     */
    var $helpers=array('Javascript','Ajax');

    /**
     * Describe listitems
     *
     * @param $committee_id
     * @return null
     */
    function listitems($committee_id){
        $list=$this->Item->find('list',array('conditions'=>array('committee_id'=>$committee_id)));
        foreach($list as $dkey=>$ddat){
            $output[]=$dkey;
            $output[]=$ddat;
        }
        $this->set('response',implode(',',$output));
        $this->render('raw');
    }

    /**
     * Describe latestitem
     *
     * @param $committee_id
     * @return null
     */
    function latestitem($committee_id){
        Configure::write('debug', 0); // dont want debug in ajax returned html
        $list=$this->Item->find('list',array('limit'=>1,'order'=>'updated desc','conditions'=>array('committee_id'=>$committee_id)));
        $this->layout='empty';
        foreach($list as $dkey=>$ddat){
            $output[]=$dkey;
            $output[]=$ddat;
        }
        $this->set('response',implode(',',$output));
        $this->render('raw');
    }

    /**
     * Describe checkemail
     *
     * @param $email
     * @return null
     */
    function checkemail($email){
        $this->set('response',$this->User->find('first',array('conditions'=>array('email'=>$email))));
        $this->render('response');
    }
	
	 /**
     * Describe check user name
     *
     * @param $name
     * @return null
     */
    
    function checkname($name){
		$name = str_replace('_slashes','/',$name);
		$name = str_replace('_',' ',$name);
        $this->set('response',$this->User->find('first',array('conditions'=>array('User.name'=>$name))));
        $this->render('response');
    }

	 /**
     * Describe checkusername
     *
     * @param $username
     * @return null
     */
    function checkusername($username){
        $this->set('response',$this->User->find('first',array('conditions'=>array('username'=>$username))));
        $this->render('response');
    }

    /**
     * Describe userlist
     *
     * @return null
     */
    function userlist(){
        $dcommittee=$this->Committee->findByShortName($this->params['committee']);
        $this->set('dcommittee',$dcommittee);
        $this->set('groups',$this->Group->find('list',array('conditions'=>array('committee_id'=>$dcommittee['Committee']['id']))));
        $this->set('users',$this->Group->User->getUsersPerCommitteeList($dcommittee['Committee']['id']));
        $this->render('userlist');
    }

    /**
     * Describe groupusers
     *
     * @param $groupid
     * @return null
     */
    function groupusers($groupid){
        $users=$this->UsersGroup->getData(array('user_id'),array('group_id'=>$groupid));
        $this->set('response',implode(',',Set::extract($users,'{n}.UsersGroup.user_id')));
        $this->render('raw');
    }

    /**
     * Describe usergroups
     *
     * @param $userid
     * @return null
     */
    function usergroups($userid){
        $users=$this->UsersGroup->getData(array('group_id'),array('user_id'=>$userid));
        $this->set('response',implode(',',Set::extract($users,'{n}.UsersGroup.group_id')));
        $this->render('raw');
    }

    /**
     * Describe calendar
     *
     * @return null
     */
    function calendar(){
        $this->Committee->Meeting->Behaviors->detach('MultiFile');
        
        if(isset($this->params['named']['year'])){
            $year=$this->params['named']['year'];
        }
        else{
            $year=date('Y');
        }
        if(isset($this->params['named']['month'])){
            $month=intval($this->params['named']['month']);
        }
        else{
            $month=intval(date('m'));
        }
        if(isset($this->params['url']['url'])){
            $a=strpos($this->params['url']['url'],'committee:');
            if(!empty($a)){
                $this->params['committee']=substr($this->params['url']['url'],$a+10);
                $this->dcommittee=$this->Committee->find('first',array('conditions'=>array('short_name'=>$this->params['committee'])));
            }
        }
        if(isset($this->params['committee'])){
            $this->set('dcommittee',$this->dcommittee);
            $meetings=$this->Committee->Meeting->find(
                'all',array(
                    'conditions'=>array(
                        'committee_id'=>$this->dcommittee['Committee']['id'],
                        'month(Meeting.meeting_date)'=>$month,
                        'year(Meeting.meeting_date)'=>$year
                    ),
                )
            );
        }else{
            $committee=$this->Committee->registeredCommittee($this->Auth->user('id'));
            $meetings=$this->Committee->Meeting->find(
                'all',array(
                    'conditions'=>array(
                        'committee_id'=>$committee,
                        'month(Meeting.meeting_date)'=>$month,
                        'year(Meeting.meeting_date)'=>$year,
                    ),
                )
            );
        }
        $this->set('meetings',$meetings);
        $this->set(compact('year','month'));
        if(isset($this->params['requested'])){
            return $this->render('calendar');
        }
    }
    
}
?>
